﻿using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Linq;

namespace Portal
{
    [ToolboxItemAttribute(false)]
    public partial class CustomerSpeak : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public CustomerSpeak()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            SPList customerSpeakList = web.GetList("/Lists/CustomerSpeak/");

            SPQuery customerQuery = new SPQuery();

            customerQuery.Query = @"<OrderBy>
                             <FieldRef Name='SpeakDate' Ascending='False' />
                            </OrderBy>";
            customerQuery.ViewFields = @"  <FieldRef Name='ID'/>
                                            <FieldRef Name='Title'/>
                                            <FieldRef Name='Description'/>                                            
                                            <FieldRef Name='SpeakDate'/>
                                            <FieldRef Name='SpeakUserName'/>                                      
                                ";
            customerQuery.RowLimit = 2;

            SPListItemCollection customerItems = customerSpeakList.GetItems(customerQuery);

            if (customerItems.Count > 0)
            {
                var customerSpeakItemsModified = from happinessItem in customerItems.GetDataTable().AsEnumerable()
                                                 select new
                                                 {
                                                     SpeakDate = "On " + Convert.ToDateTime(happinessItem["SpeakDate"].ToString()).ToString("MMM dd, yyyy"),
                                                     SpeakDescription = happinessItem["Description"].ToString().Length >= 200 ? happinessItem["Description"].ToString().Substring(0, 200) + "..." : happinessItem["Description"].ToString(),
                                                     SpeakTitle = happinessItem["Title"].ToString(),
                                                     SpeakID = happinessItem["ID"].ToString(),
                                                     SpeakUserName = happinessItem["SpeakUserName"].ToString()
                                                 };

                listCustomerSpeak.DataSource = customerSpeakItemsModified.ToList();
                listCustomerSpeak.DataBind();
            }
        }
    }
}
